package com.chao.common.util.doc;


import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件工具
 */
public class FileUtile {

    /**
     * 遍历代码文件夹 扫描接口
     */
    public static boolean delFiles(File folder){
        if (!folder.isDirectory()) {
            return folder.delete();
        }
        String[] folderList = folder.list();
        if(null == folderList){
            return folder.delete();
        }
        for (String file : folderList) {
            delFiles(new File(folder, file));
        }
        return folder.delete();
    }

    /**
     * 追加内容到文件
     */
    public static void addFile(File file,List<String> strs){
        try(Writer out =new FileWriter(file,true)) {
            StringBuilder sb = new StringBuilder();
            for (String str : strs) {
                sb.append(str).append("\r\n");
            }
            out.write(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 写入内容到文件
     */
    public static void saveFile(File file,List<String> strs){
        try(Writer out =new FileWriter(file)) {
            StringBuilder sb = new StringBuilder();
            for (String str : strs) {
                sb.append(str).append("\n");
            }
            out.write(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建多级目录文件
     * @return
     */
    public static File createFile(String path){
        File file = new File(path);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    //按照行读取文件
    public static List<String> readFileByLine(File f){
        List<String> list = new ArrayList<>();
        try (BufferedReader br=new BufferedReader(new FileReader(f))){
            String temp;
            while((temp=br.readLine())!=null){
                list.add(temp);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return list;
    }

//    public static void main(String[] args) {
//        List<String> list = readFileByLine(new File("C:\\Users\\Administrator\\Desktop\\addr"));
//        List<JSONObject> datas = main1(list,"",1);
//        System.out.println(JSONObject.toJSONString(datas));
//    }
//   省市区json格式转码
//    public static List<JSONObject> main1(List<String> list,String sss,int i) {
//        List<JSONObject> datas = new ArrayList<>();
//        for (String s:list){
//            if(s.length() < 6){
//                continue;
//            }
//            String[] ss = s.split("\t");
//            if(i == 1 && "0000".equals(s.substring(2,6))){
//                JSONObject data = new JSONObject();
//                data.put("areacode",ss[0]);
//                data.put("areaname",ss[1]);
//                data.put("subarea",main1(list,s.substring(0,2),2));
//                datas.add(data);
//                continue;
//            }
//            if(s.substring(0,2).equals(sss) && "00".equals(s.substring(4,6)) && !"0000".equals(s.substring(2,6)) && i ==2){
//                JSONObject data = new JSONObject();
//                data.put("areacode",ss[0]);
//                data.put("areaname",ss[1]);
//                data.put("subarea",main1(list,s.substring(0,4),3));
//                datas.add(data);
//                continue;
//            }
//            if(s.substring(0,4).equals(sss) && i == 3){
//                JSONObject data = new JSONObject();
//                data.put("areacode",ss[0]);
//                data.put("areaname",ss[1]);
//                datas.add(data);
//            }
//        }
//        return datas;
//    }
}
